package com.scy.face.controller;

import cn.hutool.core.util.IdUtil;
import com.arcsoft.face.FaceEngine;
import com.arcsoft.face.FaceFeature;
import com.arcsoft.face.FaceInfo;
import com.arcsoft.face.toolkit.ImageInfo;
import com.scy.face.config.R;
import com.scy.face.config.UserLibrary;
import com.scy.face.model.User;
import com.scy.face.util.FaceUtil;
import com.sun.istack.internal.NotNull;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static com.arcsoft.face.toolkit.ImageFactory.getRGBData;

/**
 * @author WH
 */

@RequestMapping("/user")
@RestController
@CrossOrigin
public class UserController {
    /**
     * 用户名密码登录
     */
    private final String UP_TYPE = "1";

    /**
     * face登录
     */
    private final String Face_TYPE = "2";

    /**
     * @param type
     * @param username
     * @param password
     * @param face
     * @return
     */
    @PostMapping("/login")
    @SneakyThrows
    public R login(@NotNull String type, String username, String password, MultipartFile face) {
        R r = new R();
        FaceEngine faceEngine = FaceUtil.getFaceEngine();
        if (UP_TYPE.equals(type)) {
            User user = UserLibrary.loginByUP(username, password);
            if (user != null) {
                r.setStatus("success");
                r.setData(user.getUsername());
            } else {
                r.setStatus("fail");
            }
        } else if (Face_TYPE.equals(type)) {
            byte[] temp = getFeatureData(face.getBytes());
            if (temp == null) {
                r.setStatus("fail");
            } else {
                User user = UserLibrary.loginByFace(temp, faceEngine);
                if (user != null) {
                    r.setStatus("success");
                    r.setData(user.getUsername());
                } else {
                    r.setStatus("fail");
                }
            }
        } else {
            r.setStatus("fail");
        }
        FaceUtil.unInit(faceEngine);
        return r;
    }

    @PostMapping("/add")
    @SneakyThrows
    public R add(@NotNull String username, @NotNull String password, @NotNull MultipartFile face) {
        String id = IdUtil.randomUUID();
        File target = new File("F:\\face", face.getOriginalFilename());
        R r = new R();
        User user = new User();
        user.setId(id);
        user.setFeatureData(getFeatureData(face.getBytes()));
        face.transferTo(target);
        user.setFilePath(target.getAbsolutePath());
        user.setUsername(username);
        user.setPassword(password);
        if (UserLibrary.add(user)) {
            r.setStatus("success");
        } else {
            r.setStatus("fail");
        }
        return r;
    }

    @GetMapping("/test")
    public List<User> test() {
        return UserLibrary.getUsers();
    }

    private byte[] getFeatureData(byte[] bytes) {
        FaceEngine faceEngine = FaceUtil.getFaceEngine();
        //人脸检测
        ImageInfo imageInfo = getRGBData(bytes);
        List<FaceInfo> faceInfoList = new ArrayList<FaceInfo>();
        faceEngine.detectFaces(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(),
                imageInfo.getImageFormat(), faceInfoList);

        if (faceInfoList == null || faceInfoList.size() == 0) {
            return null;
        }
        System.out.println(faceInfoList);
        //特征提取
        FaceFeature faceFeature = new FaceFeature();
        faceEngine.extractFaceFeature(imageInfo.getImageData(), imageInfo.getWidth(), imageInfo.getHeight(),
                imageInfo.getImageFormat(), faceInfoList.get(0), faceFeature);
        FaceUtil.unInit(faceEngine);
        return faceFeature.getFeatureData();
    }

}